//
//  JARootController.swift
//  JAWeiBo
//
//  Created by Arvin on 17/4/2.
//  Copyright © 2017年 Arvin. All rights reserved.
//

import UIKit
import MJRefresh

fileprivate let identifier = "identifer"

class JARootController: UIViewController {
    
    ///下拉刷新控件
    lazy var refreshHeader: MJRefreshNormalHeader = MJRefreshNormalHeader (refreshingTarget: self, refreshingAction: #selector(loadData))
    
    ///上拉刷新控件
    lazy var refreshFooter: MJRefreshAutoNormalFooter = MJRefreshAutoNormalFooter (refreshingTarget: self, refreshingAction: #selector(loadData))
    
    
    ///添加tableView
    lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.dataSource = self
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: identifier)
        
        return tableView
    }()

    var visitorView: JAVisitorView?
    
    //访客视图的文本的图标信息
    var visitorDic: [String: Any]?
    
    deinit {
        //移除通知
        NotificationCenter.default.removeObserver(self)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        
        //接收通知
        NotificationCenter.default.addObserver(self, selector: #selector(loginSuccess), name: loginSuccessNotification, object: nil)
    }

}

extension JARootController {
    func loadData() {
       
    }
}

// MARK: - 事件处理
extension JARootController {
    
    //登录成后的通知方法
    func loginSuccess() {
        
        visitorView?.removeFromSuperview()
        visitorView = nil
        
        //加载数据
        loadData()
    }
}

// MARK: - 设置UI
extension JARootController {
   fileprivate func setupUI() {
        
        self.automaticallyAdjustsScrollViewInsets = false
        
        //tablewView一定要创建在visitorView的下面
        setupTableView()
        
        setupVisitorView()
    }
    
    func setupTableView() {
        self.view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalTo(self.view)
            make.top.equalTo(self.view).offset(64)
        }
        
        //隐藏分隔cell的小线
        tableView.separatorStyle = .none
        
        //设置上下拉刷新控件
        tableView.mj_header = refreshHeader
        tableView.mj_footer = refreshFooter
    }
    
    ///设置访客视图
    func setupVisitorView() {
        //判断是否登录成功
        if JAUserAccount.shared.isLogin == false {
        visitorView = JAVisitorView(frame: self.view.bounds)
        visitorView?.visitorInfo = self.visitorDic
        visitorView?.delegate = self
        self.view.addSubview(visitorView!)
            
        }
    }
}

// MARK: - WBVisitorViewDelegate
extension JARootController: JAVisitorViewDelegate {
    //弹出登录界面
    func login() {
        let loginController = JALoginController()
        let loginNav = UINavigationController(rootViewController: loginController)
        present(loginNav, animated: true, completion: nil)
        
        
    }
}

// MARK: - UITableViewDataSource
extension JARootController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
        cell.textLabel?.text = indexPath.description
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return 20
    }
}
